package registnet.records.source;

import java.util.List;

import registnet.records.Grade;
import registnet.records.Gradesheet;
import registnet.records.ICourse;
import registnet.records.Section;
import registnet.records.Student;

public class GradesheetSource {

	private Section section;
	private int semester;
	private int year;
	private Gradesheet gradesheet;
	
	
	/**
	 * Default constructor
	 * 
	 * @param section - The section who owns the gradesheet
	 * @param semester - The semester when the gradesheet is used
	 * @param year - The year when the gradesheet is used
	 */
	public GradesheetSource(Section section, int semester, int year){
		this.section = section;
		this.semester = semester;
		this.year = year;
		this.load();
	}
	
	
	/**
	 * Loads data from source
	 */
	public void load(){
		try{
			registnet.dao.Course DAOCourse = new registnet.dao.CoursePeer().getCourse(this.section.getCourse().getCourseCode());
			List<registnet.dao.Grade> DAOGrades = new registnet.dao.GradePeer().getGradeBySection(DAOCourse.getCourseId(), this.section.getSectionName(), this.semester, this.year);
			this.gradesheet = new Gradesheet(this.section, this.semester, this.year);
			for(int i=0; i<DAOGrades.size(); i++){
				registnet.dao.Grade DAOGrade = DAOGrades.get(i);
				Student student = new StudentSource(DAOGrade.getStudent().getStudentNumber()).getStudent();
				ICourse course = new CourseTakenSource(student, DAOGrade.getCourse().getCourseCode(), this.section, this.semester, this.year).getCourseTaken();
				Grade grade = new GradeSource(student, course, this.semester, this.year, DAOGrade.getGrade()).getGrade();
				this.gradesheet.addGrade(grade);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Gets the gradesheet
	 * 
	 * @return	The gradesheet
	 */
	public Gradesheet getGradesheet(){
		return this.gradesheet;
	}
	
}
